import { EcsComponent } from "./EcsComponent";
import { EcsEntityGroup } from "./EcsEntityGroup";

export abstract class EcsSystem<WorldClz = any>  {
    private _enable: boolean = true;
    private _world: WorldClz;
    public defaultGroup: EcsEntityGroup = null;

    updateCount = 0

    public get world(): WorldClz {
        return this._world;
    }
    public get name(): string {
        return "";
    }
    /**
     * 
     * @param world 世界
     * @param defaltGroupComps 系统默认组关心的组件类型
     */
    constructor(world: WorldClz, defaltGroupComps: (new () => EcsComponent)[]) {
        this._world = world
        if (defaltGroupComps.length >= 0) {
            this.defaultGroup = (world as any).getGroupByCompIds(...defaltGroupComps.map(i => EcsComponent.getIdFromClz(i)));
        }
    }

    // public SetupGroup(world:EntityWorld)
    // {
    // 	//获取缓存组
    // 	//this.group = world.getEntityGroup(CompType1,CompType2);
    // 	//this.group.add
    // }

    public updateSystem(): void {
        if (this._enable && (this.world as any).enable) {
            if (this.updateCount == 0) {
                this.onFirstUpdate()
            }
            this.update();
            this.updateCount++
        }
    }

    public abstract update(): void

    public destroy(world: WorldClz): void {

    }

    public isEnable(): boolean {
        return this._enable;
    }

    public setEnable(enable: boolean): void {
        if (enable == this._enable) {
            return;
        }
        this._enable = enable;
        if (this._enable) {
            this.onEnable();
        }
        else {
            this.onDisable();
        }
    }

    protected onFirstUpdate(): void {

    }

    protected onEnable(): void {

    }

    protected onDisable(): void {

    }
}